//
//  EarningsRecordController.swift
//  SCA
//
//  Created by tjun on 2025/9/13.
//

import UIKit
import SnapKit


class EarningsRecordController: BaseController {

    private var datas: [EarningsRecordModel] = []
    
    let pool: String
    
    init(pool: String) {
        self.pool = pool
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = 40
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(EarningsRecordCell.self, forCellReuseIdentifier: EarningsRecordCell.identifier)
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: tabBarSafeBottomMargin, right: 0)
        tableView.separatorStyle = .none
        return tableView
    }()
    
    private let totalEarningsLabel = UILabel(font: UIFontMediumMake(20), textColor: .white)
    
    private lazy var emptyDataHandler = EmptyDataHandler(scrollView: tableView, configuration: { model in
        model.text = "暂无数据".localizedString
        return .onlyText
    })

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        navigationBar.title = "收益详情".localizedString
        
        let headerView = UIView().addTo(self.view)
        headerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navBarHeight)
            make.height.equalTo(80)
        }
        
        totalEarningsLabel.addTo(headerView)
        totalEarningsLabel.snp.makeConstraints { make in
            make.left.equalTo(AppStyle.margin.left + 12)
            make.top.equalTo(10)
        }
        
        let earningsRecordView = EarningsRecordView().addTo(headerView)
        earningsRecordView.dateLabel.text = "日期".localizedString
        earningsRecordView.typeLabel.text = "类型".localizedString
        earningsRecordView.amountLabel.text = "数量".localizedString
        earningsRecordView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.bottom.equalToSuperview()
            make.height.equalTo(36)
        }
        
        let lineView = UIView().addTo(headerView)
        lineView.backgroundColor = .white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(headerView.snp.bottom)
        }
        
        loadData()
    }
    
    @objc func loadData() {
        ComputingPowerService.loadEarningsDetailsData(pool: pool) { models, totalAmount in
            self.emptyDataHandler.display = true
            self.totalEarningsLabel.text = "总收益： ".localizedString + totalAmount
            self.datas.append(contentsOf: models)
            self.tableView.reloadData()
        } failure: { error in
            self.totalEarningsLabel.text = "总收益： ".localizedString + "$0.0000"
            self.emptyDataHandler.display = true
            self.tableView.reloadData()
        }
    }
 
}

//        loadCastingEarningsDetailsData

extension EarningsRecordController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: EarningsRecordCell.identifier, for: indexPath) as! EarningsRecordCell
        cell.indexPath = indexPath
        let model = datas[indexPath.row]
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
    }
    
}
